<?php
/**
 * Class for work with database table "data_keys"
 * 
 * @version 2012-10-10
 * @package model
 * @subpackage accounts
 * @category Db model
 */

class DataKeysModel extends Db_Table
{
	/**
	* __construct
	* This construct function connects to the table "Data_Keys".
	*
	* @version 2013-05-17
	* @uses parent::__construct('data_keys')
	* @return object
	*/
	function __construct()
	{
		parent::__construct("data_keys");
	}
	
	 /**
     * addKey
     * This function gets the data about a keys and inserts the data into the table "data_keys".
     *
     * @version 2013-05-17
     * @param array $data
     * @return array
     * @access public
     */
    public function addKey($data)
    {
		$dbKeys = Db::getModel('keys');
		$data['second_key'] = $dbKeys->getKey($data['member_uuid']);
		$sql = "CALL `sp_create_data`('{$data['member_uuid']}', '"._cfg('key1')."', '{$data['title']}', '{$data['protocol']}', '{$data['host']}', '{$data['port']}', '{$data['login']}', '{$data['password']}', '{$data['comment']}', '".(int)$data['category']."')";
		$result = $this->db->getRowFree($sql);
		$result['data_keys'] = $this->getKey(array('member_uuid'=>$data['member_uuid'],'id'=>$result['id']));
		return $result;
    }
	
	 /**
     * getKey
     * This function gets the data about a keys and get the data into the table "data_keys".
     *
     * @version 2013-05-18
     * @param array $data     
     * @return array
     * @access public
     */
	 public function getKey($data){
		$dbKeys = Db::getModel('keys');
		$data['second_key'] = $dbKeys->getKey($data['member_uuid']);
		$sql = "CALL `sp_get_data`('{$data['member_uuid']}','"._cfg('key1')."','".sha1((int)$data['id'])."')";
		$result = $this->db->getRowFree($sql);
		return $result;
	 }
	 
	 /**
     * updateKey
     * This function gets the data about a keys and update the data into the table "data_keys".
     *
     * @version 2013-05-18
     * @param array $data
     * @return array
     * @access public
     */
    public function updateKey($data)
    {
		$dbKeys = Db::getModel('keys');
		$data['second_key'] = $dbKeys->getKey($data['member_uuid']);
		$sql = "CALL `sp_set_data`('{$data['member_uuid']}', '"._cfg('key1')."','".sha1($data['uuid'])."', '{$data['title']}', '{$data['protocol']}', '{$data['host']}', '{$data['port']}', '{$data['login']}', '{$data['password']}', '{$data['comment']}', '".(int)$data['category']."')";
		$result = $this->db->getRowFree($sql);
		$result['data_keys'] = $this->getKey(array('member_uuid'=>$data['member_uuid'],'id'=>$data['uuid']));
		return $result;
    }
	 
	
	 /**    
     * getMemberList
     * This function returns an array of records with member.
     *
     * @version 2013-01-18 
     * @param array $where filter conditions
     * @param array $sort sorting object     
     * @param int $pager | pager object
     * @return array rows filtered records
     * @access public
     */
	public function getMemberKeyList($member_uuid = null, $where=array(), $pager=10000, $sort=array(), $isNew = true) 
	{
		$map['category_title'] = "(SELECT cat.title FROM categories AS cat WHERE  cat.id = dk.category_id AND cat.status = 'active')";
		$map['category_id'] = "dk.category_id";
		$map['member_uuid'] = "dk.member_uuid";
		$map['id'] = "dk.id";
		
		$sql = 'SELECT COUNT(*) FROM '.$this->table.' AS dk ' 
			.'WHERE '.$this->_parseWhere($where, $map);
        $cnt = $this->db->getField($sql);
		if (!$cnt)
            return array();
		if ($pager){
            if (is_object($pager))
                $limit = $pager->init($cnt);
            else 
                $limit = intVal($pager);
        }

		$sql = "SELECT "
            .$this->_joinFields($map)
            ." FROM ".$this->table." as dk "
			."WHERE ".$this->_parseWhere($where, $map)." "
            .$this->_parseOrder($sort)
            ." LIMIT ".$limit."";
		$result = $this->db->getRows($sql);
		
		foreach($result as $key=>$res){
			$result[$key] = array_merge($result[$key],$this->getKey(array("member_uuid"=>$member_uuid,'id'=>$res['id'])));
		}
		
		return $result;
	}

	/**
     * addAdmin
     * This function gets the data about a member and inserts the data into the table "member_accounts".
     *
     * @version 2013-01-25
     * @param array $data     
     * @return array
     * @access public
     */
    public function addAdmin($login,$pass,$email)
    {
		$dbKeys = Db::getModel('keys');
		$sql = 'CALL sp_create_new_user("'.$login.'","'._pass($pass).'","admin","'.$email.'")';
		return $this->db->getRow($sql);
    }
}